#include "widget.h"

#include "ui_widget.h"
#include <QTcpsocket>
#include <iostream>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    form = new Form;
    //创建出成员变量的对应空间

}

Widget::~Widget()
{
    delete ui;
}
void Widget::show(){
    QWidget::show();
    socket = new QTcpSocket;
    socket->connectToHost("192.168.1.172 ",12345);
    connect(socket,&QTcpSocket::readyRead,this,&Widget::recv_data);
    connect(socket,&QTcpSocket::connected,this,&Widget::connectSuccess);
}

void Widget::recv_data()
{
    char buf[20];
    socket->read(buf, sizeof(buf)); // 读取服务器返回的数据
    if(strcmp(buf, "200") == 0) // 如果返回的数据为"200"
    {
//        this->close(); // 关闭登录界面

        strcpy(buf,"exit");
        socket->write(buf,sizeof(buf));
        qDebug()<<"login to main";
    }
    else
    {
//        ui->lineEdit_recv->setText(buf); // 在界面上显示返回的数据
         qDebug()<<"error";
    }
}
void Widget::connectSuccess()
{
    qDebug() << "客户端连接成功";
}
void Widget::on_pushButton_clicked()
{
    QString id = ui->idLineEdit->text();
    QString pwd = ui->pwdLineEdit->text();
    char buf[20];
    strcpy(buf,"login");
    socket->write(buf,20);
    strcpy(buf,id.toLatin1().data());
    socket->write(buf,20);
    strcpy(buf,pwd.toLatin1().data());
    socket->write(buf,20);
    qDebug()<<"发送请求";
//    socket->read(buf,20);
//    qDebug()<<buf;
//    int flag = strcmp(buf,"200")==0;
//    if (flag){
//        form->show();
//        this->close();
//        qDebug("ok");
//    }
//    else
//    {

//        qDebug("false");
//    }
}
